package ICM.client;

import javax.swing.*;
import javax.swing.border.*;

import common.*;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class LogInUI extends JPanel implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextField txtLogin;
	private JPasswordField txtPassword;
	private JButton btnLogin;
	private JButton btnClose;
	private Client c;
	private ClientUI ui;
	
	public LogInUI(Client c, final ClientUI ui) {
		setMinimumSize(new Dimension(700, 600));
		this.c=c;
		this.ui=ui;
		
		setLayout(new GridBagLayout());
		
				JPanel panel = new JPanel();
				panel.setLocation(new Point(50, 50));
				panel.setSize(900,800);
				
				panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
				
				GridBagConstraints gbc_panel = new GridBagConstraints();
				gbc_panel.anchor = GridBagConstraints.NORTHWEST;
				gbc_panel.gridheight = 5;
				gbc_panel.insets = new Insets(5, 0, 5, 0);
				gbc_panel.gridx = 0;
				gbc_panel.gridy = 0;
				add(panel,gbc_panel);
				panel.setLayout(new GridBagLayout());
				
		JLabel lblICMLogin = new JLabel("ICM Login");
		lblICMLogin.setHorizontalAlignment(SwingConstants.CENTER);
		lblICMLogin.setBounds(7, 10, 70, 17);
		lblICMLogin.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		GridBagConstraints gbc_lblICMLogin = new GridBagConstraints();
		gbc_lblICMLogin.anchor = GridBagConstraints.NORTH;
		gbc_lblICMLogin.gridwidth = 2;
		gbc_lblICMLogin.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblICMLogin.insets = new Insets(5, 0, 5, 0);
		gbc_lblICMLogin.gridx = 0;
		gbc_lblICMLogin.gridy = 0;
		panel.add(lblICMLogin, gbc_lblICMLogin);
		
		JLabel lblLogin = new JLabel("Login(ID):");
		lblLogin.setHorizontalAlignment(SwingConstants.LEFT);
		lblLogin.setBounds(173, 11, 29, 14);
		GridBagConstraints gbc_lblLogin = new GridBagConstraints();
		gbc_lblLogin.fill = GridBagConstraints.BOTH;
		gbc_lblLogin.insets = new Insets(5, 20, 5, 5);
		gbc_lblLogin.gridx = 0;
		gbc_lblLogin.gridy = 1;
		panel.add(lblLogin, gbc_lblLogin);
		
		txtLogin = new JTextField();
		txtLogin.setBounds(82, 8, 86, 20);
		GridBagConstraints gbc_txtLogin = new GridBagConstraints();
		gbc_txtLogin.fill = GridBagConstraints.BOTH;
		gbc_txtLogin.insets = new Insets(0, 0, 5, 20);
		gbc_txtLogin.gridx = 1;
		gbc_txtLogin.gridy = 1;
		panel.add(txtLogin, gbc_txtLogin);
		txtLogin.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setHorizontalAlignment(SwingConstants.LEFT);
		lblPassword.setBounds(207, 11, 50, 14);
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.fill = GridBagConstraints.BOTH;
		gbc_lblPassword.insets = new Insets(5, 20, 5, 5);
		gbc_lblPassword.gridx = 0;
		gbc_lblPassword.gridy = 2;
		panel.add(lblPassword, gbc_lblPassword);
		
		txtPassword = new JPasswordField();
		txtPassword.setBounds(262, 8, 86, 20);
		GridBagConstraints gbc_txtPassword = new GridBagConstraints();
		gbc_txtPassword.fill = GridBagConstraints.BOTH;
		gbc_txtPassword.insets = new Insets(5, 0, 5, 20);
		gbc_txtPassword.gridx = 1;
		gbc_txtPassword.gridy = 2;
		panel.add(txtPassword, gbc_txtPassword);
		txtPassword.setColumns(10);
		
		btnLogin = new JButton("Login", new ImageIcon(LogInUI.class.getResource("/client/images/Login.png")));
		btnLogin.addActionListener(this);
	/*	{
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ui.SetContent(ui.Main_Screen);
			}
		});*/
		GridBagConstraints gbc_btnLogin = new GridBagConstraints();
		gbc_btnLogin.anchor = GridBagConstraints.BASELINE_LEADING;
		gbc_btnLogin.insets = new Insets(5, 0, 5, 5);
		gbc_btnLogin.gridx = 0;
		gbc_btnLogin.gridy = 3;
		panel.add(btnLogin, gbc_btnLogin);
		btnLogin.setBounds(353, 7, 57, 23);
		ui.getRootPane().setDefaultButton(btnLogin);
		
		
		btnClose = new JButton("Close" , new ImageIcon(LogInUI.class.getResource("/client/images/Cancel.png")));
		GridBagConstraints gbc_btnClose = new GridBagConstraints();
		gbc_btnClose.anchor = GridBagConstraints.BASELINE_LEADING;
		gbc_btnClose.insets = new Insets(5, 0, 5, 0);
		gbc_btnClose.gridx = 1;
		gbc_btnClose.gridy = 3;
		panel.add(btnClose, gbc_btnClose);
		btnClose.setBounds(353, 7, 57, 23);
		btnClose.setVerticalAlignment(SwingConstants.BOTTOM);
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				if (e.getSource().equals(btnClose))
				{
                     System.exit(0);
				}
			}
		});
		lblICMLogin.setVerticalAlignment(SwingConstants.BOTTOM);
	}

	
	public static boolean isNumeric(String str)
	{
	    for (char c : str.toCharArray())	    
	        if (!Character.isDigit(c)) return false;	    
	    return true;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnLogin)) {
			// Login.
			try {
				// Check if fields are not empty.
				String login = txtLogin.getText().trim();
				String pass = new String(txtPassword.getPassword()).trim();
				if (login.length() < 1 || pass.length() < 1) {
					ClientUI.ShowMessageBox("Error", "Please fill all fields!", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if(!isNumeric(login)){
					ClientUI.ShowMessageBox("Error", "Enter digits in ID field", JOptionPane.ERROR_MESSAGE);
				    return;
				    }
				// Prepare login message.
				CommandMessage msg = new CommandMessage(CommandMessage.LOGIN_MESSAGE,new String[] {login, pass});
				// Send login request.
				c.sendToServer(msg);
				Client.Login=login;
			} catch (IOException ex) {
				ex.printStackTrace();
				ClientUI.ShowMessageBox("Error", "Login Failed", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
